package org.box.demos.multithreading.wgetmt.j16mt;

import org.box.demos.multithreading.wgetmt.core.URL;

/**
 * Utility class that represents progress of the application's completion and
 * prints it into the system output stream.
 * 
 * @author <a href="mailto:alexander.box@gmail.com">Alexander Korobka</a>
 * 
 */
public class ProgressMonitor implements TaskManagerListener {

	private static final String SUCCESS_RESULT = "[%s,%s] %s %s [done]";
	private static final String SKIPPED_RESULT = "[%s,%s] %s %s [skipped]";
	private int done = 0;
	private boolean showSkipLog;
	private long time;

	
	public void  startDownload() {
		time = System.currentTimeMillis();
	}
	/*
	 * (non-Javadoc)
	 * @see org.box.demos.multithreading.wgetmt.j16mt.TaskManagerListener#onTaskCompleted(org.box.demos.multithreading.wgetmt.j16mt.TaskResult, int)
	 */
	@Override
	public void onTaskCompleted(TaskResult result, int remains) {
		done++;
		System.out.println(String.format(SUCCESS_RESULT, done, remains,
				getSpaceTabs(result), result.getUrl()));

	}
	
	/*
	 * (non-Javadoc)
	 * @see org.box.demos.multithreading.wgetmt.j16mt.TaskManagerListener#onTaskSkipped(org.box.demos.multithreading.wgetmt.j16mt.TaskResult, int)
	 */
	@Override
	public void onTaskSkipped(TaskResult result, int remains) {
		done++;
		if (showSkipLog) {
			System.out.println(String.format(SKIPPED_RESULT, done, remains,
					getSpaceTabs(result), result.getUrl()));
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.box.demos.multithreading.wgetmt.j16mt.TaskManagerListener#onTaskFailed(java.lang.String, org.box.demos.multithreading.wgetmt.core.URL, java.lang.Throwable)
	 */
	@Override
	public void onTaskFailed(String groupId, URL url, Throwable e) {
		done++;
		System.err.println(url + " (" + url.getOrigin() + ") [failure] : " + e);
	}

	/**
	 * Prints a warning message to the system error out
	 * 
	 * @param url
	 *            - the problematic url
	 * @param e
	 *            - the root cause of the error
	 */
	public void onTaskWarning(URL url, Throwable e) {
		done++;
		System.err.println(url + " [warning] : " + e);
	}

	
	public void stopDownload(double received) {
		System.out.println(String.format(
				"Download time: %d ms.; Received: %.3f Mb.", (System.currentTimeMillis() - time), received));
	}
	

	/**
	 * Resets the listener to 'Converting links' phase
	 */
	public void startLinksConvertion() {
		System.out.println("Converting links...");
	}

	/**
	 * Fires on successful task completion
	 * 
	 * @param url - a url that has been successfully processed
	 * @param remains - a number of requested urls that remains to process
	 */
	public void onContentConverted(URL url) {
		System.out.println(url);
	}

	/**
	 * The setter for the showSkipLog property
	 * 
	 * @param showSkipLog - true, if show skipped log 
	 */
	public void setShowSkipLog(boolean showSkipLog) {
		this.showSkipLog = showSkipLog;
	}

	/**
	 * Calculates left padding of url in output according to url's recursive level
	 * 
	 * @param result - an aggregated result of the task exception
	 * 
	 * @return a string with tabs
	 */
	private String getSpaceTabs(TaskResult result) {
		StringBuilder tabs = new StringBuilder();
		for (int i = 1; i < J16MTEngine.getUrlLevel(result); i++) {
			tabs.append("\t");
		}
		return tabs.toString();
	}
}
